package com.ruitu.router_module.util;

import com.ruitu.router_module.Logg;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

// 响应拦截器
public class Health2Interceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        try {
            Response response = chain.proceed(chain.request());

            Logg.i("code     =  : " + response.code());
            Logg.i("message  =  : " + response.message());
            Logg.i("protocol =  : " + response.protocol());

            if (response.body() != null && response.body().contentType() != null) {
                MediaType mediaType = response.body().contentType();
                String string = response.body().string();
                Logg.i("mediaType =  :  " + mediaType.toString());
                Logg.i("string    =  : " + string);// 原始响应的字符串

                // 依据解密算法,解密字符串

                ResponseBody responseBody = ResponseBody.create(mediaType, string);
                return response.newBuilder().body(responseBody).build();
            } else {
                return response;
            }
        } catch (Exception e) {
            // 出现拦截异常,按照原请求继续执行
            return chain.proceed(chain.request());
        }
    }
}
